package com.glela.seckill.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.glela.goods.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.glela.common.util.DateUtil;
import com.glela.goods.mapper.ActivityDetailMapper;
import com.glela.goods.mapper.ActivityMapper;
import com.glela.goods.mapper.BrandMapper;
import com.glela.goods.mapper.GoodsCommentMapper;
import com.glela.goods.mapper.GoodsDetailMapper;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.mapper.GoodsNavigateMapper;
import com.glela.goods.mapper.GoodsServicesMapper;
import com.glela.goods.mapper.InventoryMapper;
import com.glela.goods.mapper.ProductMapper;
import com.glela.goods.mapper.ShareGoodsMapper;
import com.glela.goods.model.vo.ProductVo;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.seckill.service.SeckillService;

@Service("seckillService")
@Scope("singleton")
public class SeckillServiceImpl implements SeckillService {

	@Autowired
	private ActivityMapper activityMapper;
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private GoodsCommentMapper goodsCommentMapper;

	@Autowired
	private BrandMapper brandMapper;
	
	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private InventoryMapper inventoryMapper;
	
	@Autowired
	private GoodsDetailMapper goodsDetailMapper;
	
	@Autowired
	private ShareGoodsMapper shareGoodsMapper;
	
	@Autowired
	private GoodsNavigateMapper goodsNavigateMapper;
	
	@Autowired
	private GoodsServicesMapper goodsServicesMapper;

	@Autowired
	private ActivityDetailMapper activityDetailMapper;
	
	/**
	 * 秒杀详情
	 */
	@Override
	public ResponseEntity<?> seckillDetail(long activityId) {
		// TODO Auto-generated method stub
		ResponseSecKill resModel = new ResponseSecKill();
		// Activity secKill = activityMapper.selectActivityByPrimaryKey(activityId);
		Activity secKill = activityMapper.selectActivityWithNum(activityId);
		// 查询商品信息
		Goods goods = goodsMapper.selectGoodsById(secKill.getGoodId());
		if (goods == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20401), HttpStatus.OK);
		}
				
		// 商品介绍图
		List<GoodsDetail> goodsDetailList = goodsDetailMapper.selectGoodsDetailByGoodId(secKill.getGoodId(), GoodsImgType.ALL.getType());
		// 商品轮播图
		List<GoodsNavigate> goodsNavigate =  goodsNavigateMapper.selectGoodsNavigateByGoodId(secKill.getGoodId());
		// 商品服务
		String[] storeServiceIds = goods.getServiceDesc().split(",");
		List<Integer> intServiceIds = new ArrayList<Integer>();
		for (String serviceId : storeServiceIds) {
			intServiceIds.add(Integer.parseInt(serviceId));
		}
		List<GoodsServices> goodsServices  = goodsServicesMapper.findGoodsServicesListByIdList(intServiceIds);
		List<SpecDesc> specDescList = JSON.parseArray(goods.getSpecDesc(), SpecDesc.class);
		resModel.setGoodsId(goods.getId());
		resModel.setGoodsName(goods.getName());
		resModel.setSuggestPrice(goods.getSuggestedPrice());
		resModel.setSecKillPrice(secKill.getSalePrice());
		resModel.setBeginTime(secKill.getStartTime());
		resModel.setBeginTime(secKill.getStartTime());
		resModel.setEndTime(secKill.getEndTime());
		resModel.setLableName(goods.getLabelName());
		resModel.setBalanceNum(secKill.getLastNum());
		resModel.setTotalNum(secKill.getTotalInventoryNum());
		List<GoodsDetail> detailsList = new ArrayList<GoodsDetail>();
		List<GoodsDetail> specList = new ArrayList<GoodsDetail>();
		List<GoodsDetail> noticesList = new ArrayList<GoodsDetail>();
		for (GoodsDetail goodsDetail: goodsDetailList) {
			//详情
			if (goodsDetail.getImgType()==GoodsImgType.DETAIL.getType()){
				detailsList.add(goodsDetail);
			}else if (goodsDetail.getImgType()==GoodsImgType.SPEC.getType()){
				specList.add(goodsDetail);
			}else if (goodsDetail.getImgType()==GoodsImgType.NOTICE.getType()){
				noticesList.add(goodsDetail);
			}
		}
		resModel.setDetailList(detailsList);
		resModel.setGoodSpecImgList(specList);
		resModel.setGoodsNoticeImgList(noticesList);
		resModel.setNavigateList(goodsNavigate);
		resModel.setGoodsService(goodsServices);
		resModel.setSpecDescList(specDescList);
		
		
		// 商品推荐数
		resModel.setRecommendNum(shareGoodsMapper.selectShareGoodByGoodIdCount(goods.getId()));
		
		resModel.setCommentNum(goodsCommentMapper.selectGoodsCommentByGoodIdCount(goods.getId()));
		
		Brand brand = brandMapper.selectBrandById(goods.getBrandId());
		resModel.setBrand(brand);
		
		long startTime = DateUtil.parse(secKill.getStartTime()).getTime();
		long endTime = DateUtil.parse(secKill.getEndTime()).getTime();
		long currentTime = System.currentTimeMillis();
		
		if (currentTime < startTime) {
			resModel.setActivityStatus(1); // 即将开始
			resModel.setTimeSecond((startTime-currentTime)/1000);
		}
		
		if (startTime < currentTime && currentTime < endTime) {
			resModel.setActivityStatus(2); // 活动进行中
			resModel.setTimeSecond((endTime-currentTime)/1000);
		}
		
		if (currentTime > endTime) {
			resModel.setActivityStatus(3); // 活动结束
			resModel.setTimeSecond(0);
		}
		
		return new ResponseEntity<>(new ResponseResultModel<>(resModel), HttpStatus.OK);
	}

	@Override
	public ResponseEntity<?> indexSeckill() {
		// TODO Auto-generated method stub
		List<ResponseSeckillList> resModel = activityMapper.selectIndexSeckillActivity();
		if (resModel == null) {
			return new ResponseEntity<>(new ResponseResultModel<>(new ArrayList<ResponseSeckillList>()), HttpStatus.OK);
		}
		return new ResponseEntity<>(new ResponseResultModel<>(resModel), HttpStatus.OK);
	}

	@Override
	public ResponseEntity<?> moreSeckillInfo(int startIndex, int pageSize, int status) {
		// TODO Auto-generated method stub
		List<ResponseSeckillList> resModel = null;
		// 抢购中
		if (status == 1) {
			resModel = activityMapper.selectMoreInfoSeckill(startIndex,pageSize);
		}
		// 即将开始
		if (status == 2) {
			resModel = activityMapper.selectMoreInfoSeckillWait(startIndex,pageSize);
		}
		if (resModel == null) {
			return new ResponseEntity<>(new ResponseResultModel<>(new ArrayList<ResponseSeckillList>()), HttpStatus.OK);
		}
		return new ResponseEntity<>(new ResponseResultModel<>(resModel), HttpStatus.OK);
	}

	@Override
	public ResponseEntity<?> decideRepertory(long activityId, long goodsId, String specInfo) {
		// TODO Auto-generated method stub
		Activity activity = activityMapper.selectActivityByPrimaryKey(activityId);
		if (null == activity) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C60001), HttpStatus.OK);
		}
		Goods goods = goodsMapper.selectGoodsById(goodsId);
		if (goods == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20401), HttpStatus.OK);
		}
		Product product = productMapper.selectProductByGoodIdAndSpecInfo(goodsId, specInfo);
		if (product == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20402), HttpStatus.OK);
		}
		ActivityDetail activityDetail = activityDetailMapper.selectActivityDetailById(activityId, product.getId());
		if (activityDetail == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C60003), HttpStatus.OK);
		}
		if (activityDetail.getInventoryNum() == 0) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C60003), HttpStatus.OK);
		}
		Inventory inventory = inventoryMapper.selectByPrimaryKey(activityDetail.getInventoryId());
		if (inventory == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C60003), HttpStatus.OK);
		}
		ProductVo vo = new ProductVo(product);
		vo.setSalePrice(activity.getSalePrice());// 实际销售价(单位分)
		vo.setSettlementPrice(inventory.getSettlementPrice());// 结算成本价(单位分)
		vo.setInventoryNum(activityDetail.getInventoryNum());
		vo.setInventoryId(activityDetail.getInventoryId());
		List<SpecificationDetailVo> specDescList = JSON.parseArray(product.getSpecDesc(), SpecificationDetailVo.class);
		vo.setSpecDescList(specDescList);
		StringBuffer specName = new StringBuffer();
		// 冗余出这个规格 的名字组合
		for (SpecificationDetailVo spec : specDescList) {
			specName.append(spec.getDetailName()).append("  ");
		}
		vo.setSpecName(specName.toString());
		
		/*Map<String,Object> resModel = new HashMap<String,Object>();
		resModel.put("inventoryNum", activityDetail.getInventoryNum());*/
		return new ResponseEntity<>(new ResponseResultModel<>(vo), HttpStatus.OK);
	}

}
